package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Teacher_Information {
    ResultSet result = null;
    private String id;
    private String name;
    private String pos;
    private String course;
    private JFrame userview;
    private Connection connection;
    public JFrame teacherView = new JFrame();
    private JPanel panel = new JPanel(new GridLayout());
    JTextField textField_tea = new JTextField();
    JTextField textField_pos = new JTextField();
    JTextField textField_account = new JTextField();
    JButton submit = new JButton("提交");
    JButton Return = new JButton("返回");
    JComboBox<String> comboBox;
    int coursenumber;

    public Teacher_Information(Connection connection, String id, JFrame userview) {
        this.connection = connection;
        this.userview = userview;
        this.id = id;
        init();
    }

    void init() {
        teacherView = new JFrame();
        teacherView.setTitle("老师信息查询");
        teacherView.setSize(500, 500);
        teacherView.setLocationRelativeTo(null);
        teacherView.setVisible(true);
        GridLayout layout = new GridLayout(5, 2);
        JPanel panel = new JPanel(layout);//设置为layout布局
        JLabel L_tea = new JLabel("姓名");
        JLabel L_pos = new JLabel("职位");
        JLabel L_account = new JLabel("账户");
        JLabel L_choose = new JLabel("讲授的课程");

        textField_account.setEditable(false);
        L_tea.setFont(new Font("", Font.BOLD, 22));
        L_pos.setFont(new Font("", Font.BOLD, 22));
        L_account.setFont(new Font("", Font.BOLD, 22));
        L_choose.setFont(new Font("", Font.BOLD, 12));
        String[] listData = {"无", "大数据技术", "算法分析与设计", "Kotlin语言", "汇编语言"};
        comboBox = new JComboBox<String>(listData);
        course=returnCourse();
        System.out.println(course);
        System.out.println(coursenumber);
        coursenumber=returnNumber(course);
        comboBox.setSelectedIndex(coursenumber);
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                String lesson = (String) comboBox.getSelectedItem();
                try {
                    String sql = "update 老师基本信息表 set course=? where teacherid=?";
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1, lesson);
                    pstmt.setString(2, id);
                    pstmt.executeUpdate();
                    int number=returnNumber(lesson);
                    coursenumber=number;

                } catch (SQLException ex) {
                }
            }
        });

        textField_pos.setFont(new Font("", Font.BOLD, 22));
        textField_tea.setFont(new Font("", Font.BOLD, 22));
        textField_account.setFont(new Font("", Font.BOLD, 22));
        submit.setFont(new Font("", Font.BOLD, 22));
        Return.setFont(new Font("", Font.BOLD, 22));
        panel.add(L_account);
        panel.add(textField_account);
        panel.add(L_pos);
        panel.add(textField_pos);
        panel.add(L_tea);
        panel.add(textField_tea);
        panel.add(L_choose);
        panel.add(comboBox);
        panel.add(submit);
        panel.add(Return);
        teacherView.add(panel);
        teacherView.setVisible(true);
        getInformation();
        function();
    }

    public void getInformation() {
        try {
            PreparedStatement stmt = null;
            String sql = "select * from 老师基本信息表 where teacherid=?";
            stmt = connection.prepareStatement(sql);
            stmt.setString(1, id);
            result = stmt.executeQuery();
            System.out.println(result);
            if (result.next()) {
                textField_account.setText(result.getString(1));
                textField_tea.setText(result.getString(3));
                textField_pos.setText(result.getString(2));
            }
        } catch (Exception eee) {
        }
    }
    public int returnNumber(String course){
        if(course.equals("汇编语言"))
            return 4;
        else if(course.equals("大数据技术"))
            return 1;
        else if(course.equals("算法分析与设计"))
            return 2;
        else if(course.equals("Kotlin语言"))
            return 3;
        else
            return 0;
    }
    public String returnCourse(){
        try{
            String sql = "select course from 老师基本信息表 where teacherid = ?";
            System.out.println("1");
            PreparedStatement stmt1= connection.prepareStatement(sql);
            System.out.println("1");
            stmt1.setString(1, id);
            System.out.println(id);
            ResultSet resultSet= stmt1.executeQuery();
            if(resultSet.next()){
                return resultSet.getString(1);
            }
            return "无";
        }catch (Exception e){
            return "无";
        }
    }

    public void function() {
        submit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    id = textField_account.getText();
                    name = textField_tea.getText();
                    pos = textField_pos.getText();
                    String sql = "update 老师基本信息表 set name=?,position=? where teacherid=?";//
                    PreparedStatement pstmt = connection.prepareStatement(sql);
                    pstmt.setString(1, name);
                    pstmt.setString(2, pos);
                    pstmt.setString(3, id);
                    int count = pstmt.executeUpdate();
                    if (count == 1) {
                        JOptionPane.showMessageDialog(teacherView, "提交成功", "Yes", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch (Exception ee) {

                }
            }
        });
        Return.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userview.setVisible(true);
                teacherView.dispose();
            }
        });
    }


}
